<template>
  <Dialog title="修改角色名" :visible.sync="dialogVisible" @submit="submit">
    <el-form :model="formData" label-width="80px">
      <el-form-item label="角色名" required>
        <el-input
          @keyup.enter.native="submit"
          v-model.trim="formData.powerName"
        ></el-input>
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script>
import { inOrUpRole } from "@/api/role";

export default {
  name: "EditRoleName",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    roleInfo: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      //对话框状态--------------------------------------------
      dialogVisible: this.visible,
      //表单对象--------------------------------------------
      formData: {
        powerName: "",
        uuid: ""
      }
    };
  },

  watch: {
    //监听对话框状态--------------------------------------------
    dialogVisible(newVal) {
      this.$emit("update:visible", newVal);
    }
  },

  mounted() {
    this.$set(this.formData, "powerName", this.roleInfo.powerName);
    this.$set(this.formData, "uuid", this.roleInfo.uuid);
  },

  methods: {
    submit() {
      const data = {
        ...this.formData
      };
      inOrUpRole(data).then(res => {
        console.log(res);
        if (res.state === 100) {
          this.$message({ message: res.msg, type: "success" });
        }
        this.dialogVisible = false;
        this.$emit("updateData");
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
